package cn.itcast.web.controller.company;

import cn.itcast.domain.company.Company;

import cn.itcast.service.company.CompanyService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

@Controller
@RequestMapping("company")
public class CompanyController extends BaseController {
    @Reference
    private CompanyService companyService;

    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize) {
        PageInfo<Company> pageInfo = companyService.findByPage(pageNum, pageSize);
        System.out.println(pageInfo);
        request.setAttribute("pageInfo",pageInfo);
        return "/company/company-list";
    }
    @RequestMapping("toAdd")
    public String toAdd(){
        return "/company/company-add";
    }

    @RequestMapping("toUpdate")
    public ModelAndView toUpdate(String id){
        System.out.println("id="+id);
        ModelAndView mv = new ModelAndView();
        Company company = companyService.findById(id);
        mv.addObject("company",company);
        mv.setViewName("/company/company-update");
        return mv;
    }

    @RequestMapping("edit")
    public String edit(Company company){
        if (StringUtils.isEmpty(company.getId())){
            Integer save = companyService.save(company);
            System.out.println("添加成功："+save+"行受到影响");
        }else {
            Integer update = companyService.update(company);
            System.out.println("修改成功 ："+update+"行受到影响");
        }
        return "redirect:/company/list.do";
    }

    @RequestMapping("delete")
    public String delete(String id){
        Integer delete = companyService.delete(id);
        System.out.println("删除成功:"+delete+"行数据受影响");
        return "redirect:/company/list.do";
    }

    @RequestMapping("/serch")
    public String formateDate(Date date){
        return "redirect:/login.do";
    }





}
